#!/usr/bin/python
# -*- coding:utf-8 -*-
from sqlalchemy import Column, Integer, String, Text, ForeignKey, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref

Base = declarative_base()


class Admin(Base):
    __tablename__ = 'admins'

    id = Column(Integer, primary_key=True)
    username = Column(String)
    password = Column(String)
    salt = Column(String)

    def __init__(self, username, password, salt):
        self.username = username
        self.password = password
        self.salt = salt


class Authority(Base):
    __tablename__ = 'authorities'

    id = Column(Integer, primary_key=True)
    name = Column(String)

    def __init__(self, name):
        self.name = name


class Incompatible(Base):
    __tablename__ = 'incompatibles'

    id = Column(Integer, primary_key=True)
    one_id = Column(Integer, ForeignKey('roles.id'))
    another_id = Column(Integer, ForeignKey('roles.id'))

    one = relationship('Role', foreign_keys=one_id)
    another = relationship('Role', foreign_keys=another_id)

    def __init__(self, one_id, another_id):
        self.one_id = one_id
        self.another_id = another_id


class Level(Base):
    __tablename__ = 'levels'

    id = Column(Integer, primary_key=True)
    high_id = Column(Integer, ForeignKey('roles.id'))
    low_id = Column(Integer, ForeignKey('roles.id'))

    high = relationship('Role', backref='levelHigh', foreign_keys=high_id)
    low = relationship('Role', backref='levelLow', foreign_keys=low_id)

    def __init__(self, high_id, low_id):
        self.high_id = high_id
        self.low_id = low_id


class Role(Base):
    __tablename__ = 'roles'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    content = Column(Text)
    app_flag = Column(Integer)

    def __init__(self, name, content, app_flag):
        self.name = name
        self.content = content
        self.app_flag = app_flag


class RoleAuthority(Base):
    __tablename__ = 'roles_authorities'

    id = Column(Integer, primary_key=True)
    role_id = Column(Integer, ForeignKey('roles.id'))
    authority_id = Column(Integer, ForeignKey('authorities.id'))

    role = relationship('Role', backref='roleAuthority')
    authority = relationship('Authority', backref='roleAuthority')

    def __init__(self, role_id, authority_id):
        self.role_id = role_id
        self.authority_id = authority_id


class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    username = Column(String)
    password = Column(String)
    salt = Column(String)

    def __init__(self, username, password, salt):
        self.username = username
        self.password = password
        self.salt = salt


class UserRole(Base):
    __tablename__ = 'users_roles'

    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey('users.id'))
    role_id = Column(Integer, ForeignKey('roles.id'))

    user = relationship('User', backref=backref('userRole', order_by=id))
    role = relationship('Role', backref=backref('userRole', order_by=id))

    def __init__(self, user_id, role_id):
        self.user_id = user_id
        self.role_id = role_id


class RLog(Base):
    __tablename__ = 'rlogs'

    id = Column(Integer, primary_key=True)
    tablename = Column(String)
    operation = Column(Integer)
    content = Column(String)
    time = Column(DateTime)

    def __init__(self, tablename, operation, content, time):
        self.tablename = tablename
        self.operation = operation
        self.content = content
        self.time = time


class AppLog(Base):
    __tablename__ = 'applogs'

    id = Column(Integer, primary_key=True)
    username = Column(String)
    operation = Column(Integer)
    time = Column(DateTime)

    def __init__(self, username, operation, time):
        self.username = username
        self.operation = operation
        self.time = time


